package org.act.neo4j.temporal.demo.vo;

import org.neo4j.graphdb.Node;

import java.util.*;

/**
 * Created by song on 16-1-22.
 */
public class Cross {

    public static int count=0;
    public final static Map<String,Cross> crossMap = new HashMap<>();
    public final String id;
    private long nodeId;
    private Node node;

    public static Cross getInCross(RoadChain roadChain){
        String crossId = getInCrossId(roadChain);
        if(crossMap.get(crossId)==null){
            crossMap.put(crossId,new Cross(crossId));
        }
        return crossMap.get(crossId);
    }

    public static Cross getOutCross(RoadChain roadChain){
        String crossId = getOutCrossId(roadChain);
        if(crossMap.get(crossId)==null){
            crossMap.put(crossId,new Cross(crossId));
        }
        return crossMap.get(crossId);
//        inSet.retainAll(outSet);// inSet now contains only elements in both sets
//        if(inSet.size()>0){
//            System.out.println("has intersection");
//        }

//        System.out.println("ONE OF OUT CHAINS:"+outRoadChain1);
//        System.out.println("BEFORE:"+roadChainSet);
//        System.out.println("ADD:"+outRoadChain1);

//        int tmp = inOutSet.size();
//
//        if(roadChainSet.size()==tmp){
//            count++;
//        }
////        System.out.println("AFTER:"+roadChainSet);
    }

    public static String getOutCrossId(RoadChain roadChain){
        Set<RoadChain> inOutSet = new HashSet<>();
        inOutSet.addAll(roadChain.outChains);
        inOutSet.addAll(roadChain.outChains.get(0).inChains);
        List<RoadChain> roadChains = new ArrayList<>( inOutSet );

        roadChains.sort(new Comparator<RoadChain>() {
            @Override
            public int compare(RoadChain o1, RoadChain o2) {
                return o1.index > o2.index ? 1 : 0;
            }
        });

        String[] roadChainIds = new String[roadChains.size()];
        for(int i=0;i<roadChains.size();i++){
            roadChainIds[i] = roadChains.get(i).index+"";
        }
        String crossId=String.join(",",roadChainIds);
        return crossId;
    }

    public static String getInCrossId(RoadChain roadChain){
        Set<RoadChain> inOutSet = new HashSet<>();
        inOutSet.addAll(roadChain.inChains);
        inOutSet.addAll(roadChain.inChains.get(0).outChains);
        List<RoadChain> roadChains = new ArrayList<>( inOutSet );

        roadChains.sort(new Comparator<RoadChain>() {
            @Override
            public int compare(RoadChain o1, RoadChain o2) {
                return o1.index > o2.index ? 1 : 0;
            }
        });

        String[] roadChainIds = new String[roadChains.size()];
        for(int i=0;i<roadChains.size();i++){
            roadChainIds[i] = roadChains.get(i).index+"";
        }
        String crossId=String.join(",",roadChainIds);
        return crossId;
    }

    private Cross(String crossId) {
        this.id = crossId;
    }

    public void setNodeId(long nodeId) {
        this.nodeId = nodeId;
    }

    public void setNode(Node node) {
        setNodeId(node.getId());
    }

    public Node getNode() {
        throw new RuntimeException("feature not completed.");
//        return node;
    }

    public static int getTotalCount() {
        return crossMap.size();
    }

    public long getNodeId() {
        return nodeId;
    }
}
